pub fn max_profit(prices: Vec<i32>) -> i32 {
    // let mut profits = vec![];
    let mut max_profit = 0;
    let mut min_buy = 0;
    for i in 0..prices.len() {
        let buy = prices[i];
        if min_buy == 0 {
            min_buy = prices[i];
        } else if buy >= min_buy {
            continue;
        } else if buy < min_buy {
            min_buy = buy;
        }
        for j in i..prices.len() {
            if prices[j] - buy > max_profit {
                max_profit = prices[j] - buy;
            }
        }
    }
    max_profit
}

pub fn max_profit_2(prices: Vec<i32>) -> i32 {
    // let mut profits = vec![];
    let mut max_profit = 0;
    let mut min_buy = i32::MAX;

    for price in prices {
        max_profit = if price - min_buy > max_profit {
            price - min_buy
        } else {
            max_profit
        };
        min_buy = if min_buy > price { price } else { min_buy };
    }

    max_profit
}
